package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.AdviceTags;
import com.dnjn.happiness.biz.domain.AdviceUserTag;
import com.dnjn.happiness.biz.service.IAdviceTagsService;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 模块功能Controller
 *
 * @author happiness
 * @date 2023-10-14
 */
@Tag(name = "biz AviceaTags 新增模块功能", description = "模块功能")
@RestController
@RequestMapping("/myAviceaTags")
public class AdviceTagsController extends BaseController {
    @Autowired
    private IAdviceTagsService adviceTagsService;


    /**
     * 查询模块功能列表
     */
    @RequiresPermissions("biz:myAviceaTags:list")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody AdviceTags domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {

        if (Objects.isNull(domain.getDeptId())) {
            domain.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        }

        Page<AdviceTags> list = adviceTagsService.selectAdviceTags(domain, Page.of(current, size).addOrder(OrderItem.desc("advice_tags_id")));
        List<AdviceTags> adviceTags = list.getRecords();
        adviceTags.stream().forEach(t -> {
            List<AdviceUserTag> userTagList = t.getUserTagList();
            String str = "";
            for (AdviceUserTag userTag : userTagList) {
                String username = userTag.getUsername();
                str = str + username + ",";
            }
            t.setUsernames(str);
        });

        return success(list);


    }

    @Operation(summary = "查询模块列表,首先新增建议的时候调用一下这个接口,新增的时候前端(或者小程序)绑定adviceTagsId字段即可")
    @GetMapping("/listALL")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult listALL() {

        Long deptId =SecurityUtils.getLoginUser().getDeptId();
        List<AdviceTags> list = adviceTagsService.selectByDeptId(deptId);
        return success(list);
    }

    /**
     * 导出模块功能列表
     */
    @RequiresPermissions("biz:myAviceaTags:export")
    @Log(title = "模块功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdviceTags adviceTags) {
        List<AdviceTags> list = adviceTagsService.list(Wrappers.query(adviceTags));
        ExcelUtil<AdviceTags> util = new ExcelUtil<AdviceTags>(AdviceTags.class);
        util.exportExcel(response, list, "模块功能数据");
    }

    /**
     * 获取模块功能详细信息
     */
    @RequiresPermissions("biz:myAviceaTags:query")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{adviceTagsId}")
    public AjaxResult get(@PathVariable("adviceTagsId") Long adviceTagsId) {
        AdviceTags AdviceTags = adviceTagsService.getAdviceTagsById(adviceTagsId);
        return AjaxResult.success(AdviceTags);
    }

    /**
     * 新增模块功能
     */
    @RequiresPermissions("biz:myAviceaTags:add")
    @Log(title = "模块功能", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AdviceTags adviceTags) {
        //检验
        Assert.notNull(adviceTags.getLabel(), "建议模块名称不能为空");
        List<AdviceTags> list = adviceTagsService.lambdaQuery().eq(AdviceTags::getLabel, adviceTags.getLabel()).list();
        if (list.size() > 0) {
            return AjaxResult.success("建议模块名称已经存在");
        }
        adviceTags.setCreateBy(SecurityUtils.getUsername());
        return toAjax(adviceTagsService.saveTags(adviceTags));

    }

    /**
     * 修改模块功能
     */
    @RequiresPermissions("biz:myAviceaTags:edit")
    @Log(title = "模块功能", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody AdviceTags adviceTags) {
        Assert.notNull(adviceTags.getLabel(), "建议模块名称不能为空");
        return toAjax(adviceTagsService.updateAdviceTagById(adviceTags));
    }


    @RequiresPermissions("biz:myAviceaTags:remove")
    @Log(title = "模块功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{adviceTagsIds}")
    public AjaxResult remove(@PathVariable Long adviceTagsIds) {
        return toAjax(adviceTagsService.lambdaUpdate().set(AdviceTags::getDeleted, "1").eq(AdviceTags::getAdviceTagsId, adviceTagsIds).update());
    }


    @Operation(summary = "第一版本-统计-建议模块列表")
    @RequiresPermissions("biz:statistical:list")
    @GetMapping("/selectAdviceTag")
    public AjaxResult adviceList() {
        List<AdviceTags> list = adviceTagsService.queryList();
//        List<AdviceTags> list = adviceTagsService.lambdaQuery().eq(AdviceTags::getDeleted, '0').list();
        return success(list);

    }

}
